package com.example.myapplication;

import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;
import com.nostra13.universalimageloader.core.listener.SimpleImageLoadingListener;

import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

/**
 * Created by xuanyang on 2017/11/20.
 */

public class ImageListActivity extends Activity {
    protected ImageLoader imageLoader = ImageLoader.getInstance();
    DisplayImageOptions options; // DisplayImageOptions是用于设置图片显示的类

    String[] imageUrls; // 图片路径
    protected AbsListView listView;

    // 一堆图片链接
    public static final String[] IMAGES = new String[] {
            // Heavy images
            "http://img.mukewang.com/55237dcc0001128c06000338-300-170dd.jpg",
            "http://img.mukewang.com/55237dcc0001128c06000338.jpg",
            "http://img.mukewang.com/55249cf30001sdffae8a06000338-300-170.jpg",
            "http://img.mukewang.com/55249csfsf30001ae8a06000338.jpg",
            "http://img.mukewang.com/5523711700016d1606000338-300-170.jpg",
            "http://img.mukewang.com/5523711700016d1606000338.jpg",
            "http://img.mukewang.com/551e470500018dd806000338-300-170.jpg",
            "http://img.mukewang.com/551e470500018dd806000338.jpg",
            "http://img.mukewang.com/551de0570001134f06000338-300-170.jpg",
            "http://img.mukewang.com/551de0570001134f06000338.jpg",
            "http://img.mukewang.com/552640c300018a9606000338-300-170.jpg"

    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ac_image_list);

//        Bundle bundle = getIntent().getExtras();
        imageUrls = IMAGES;

        // 使用DisplayImageOptions.Builder()创建DisplayImageOptions
        options = new DisplayImageOptions.Builder()
                .showStubImage(R.drawable.ic_stub) // 设置图片下载期间显示的图片
                .showImageForEmptyUri(R.drawable.ic_empty) // 设置图片Uri为空或是错误的时候显示的图片
                .showImageOnFail(R.drawable.ic_error) // 设置图片加载或解码过程中发生错误显示的图片
                .cacheInMemory(true) // 设置下载的图片是否缓存在内存中
                .cacheOnDisc(true) // 设置下载的图片是否缓存在SD卡中
                .displayer(new RoundedBitmapDisplayer(20)) // 设置成圆角图片
                .build(); // 创建配置过得DisplayImageOption对象

        listView = (ListView) findViewById(android.R.id.list);
        ((ListView) listView).setAdapter(new ItemAdapter());
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
//                // 点击列表项转入ViewPager显示界面
//                Intent intent = new Intent(ImageListActivity.this, ImagePagerActivity.class);
//                intent.putExtra(Extra.IMAGES, imageUrls);
//                intent.putExtra(Extra.IMAGE_POSITION, position);
//                startActivity(intent);
            }
        });
    }

    @Override
    public void onBackPressed() {
        AnimateFirstDisplayListener.displayedImages.clear();
        super.onBackPressed();
    }


    /**
     * 自定义列表项适配器
     */
    class ItemAdapter extends BaseAdapter {

        private ImageLoadingListener animateFirstListener = new AnimateFirstDisplayListener();

        private class ViewHolder {
            public TextView text;
            public TextView content;
            public ImageView image;
        }

        @Override
        public int getCount() {
            return imageUrls.length;
        }

        @Override
        public Object getItem(int position) {
            return position;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(final int position, View convertView,
                            ViewGroup parent) {
            View view = convertView;
            final ViewHolder holder;
            if (convertView == null) {
                view = getLayoutInflater().inflate(R.layout.item_list_image,
                        parent, false);
                holder = new ViewHolder();
                holder.text = (TextView) view.findViewById(R.id.title);
                holder.content = (TextView) view.findViewById(R.id.content);
                holder.content.setText("hello world");
                holder.image = (ImageView) view.findViewById(R.id.image);
                view.setTag(holder); // 给View添加一个格外的数据
            } else {
                holder = (ViewHolder) view.getTag(); // 把数据取出来
            }

            holder.text.setText("Item " + (position + 1)); // TextView设置文本

            /**
             * 显示图片 参数1：图片url 参数2：显示图片的控件 参数3：显示图片的设置 参数4：监听器
             */
            imageLoader.displayImage(imageUrls[position], holder.image,
                    options, animateFirstListener);

            return view;
        }
    }

    /**
     * 图片加载第一次显示监听器
     */
    private static class AnimateFirstDisplayListener extends
            SimpleImageLoadingListener {

        static final List<String> displayedImages = Collections
                .synchronizedList(new LinkedList<String>());

        @Override
        public void onLoadingComplete(String imageUri, View view,
                                      Bitmap loadedImage) {
            if (loadedImage != null) {
                ImageView imageView = (ImageView) view;
                // 是否第一次显示
                boolean firstDisplay = !displayedImages.contains(imageUri);
                if (firstDisplay) {
                    // 图片淡入效果
                    FadeInBitmapDisplayer.animate(imageView, 500);
                    displayedImages.add(imageUri);
                }
            }
        }

    }
}
